/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Interface.Windows;

import Controller.GetGoogleMap;
import Interface.Panel.PanelReforestationZoneRegister;
import java.awt.Component;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

/**
 *
 * @author USUARIO
 */
public class AreaControl extends javax.swing.JFrame {
    private Component frame;

    /**
     * Creates new form AreaControl
     */
    private PanelReforestationZoneRegister parent;
    public AreaControl(PanelReforestationZoneRegister parent) {
        this.setLocationRelativeTo(null);
        initComponents();
        tfCoorX.setText("-74.12");
        tfCoorY.setText("4.47");
        tfZoom.setText("11");
        GetGoogleMap a = new GetGoogleMap();
        lblMap.setIcon(new ImageIcon(a.GetMap(tfCoorX.getText(), tfCoorY.getText(), tfZoom.getText())));
        this.parent = parent;
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblMap = new javax.swing.JLabel();
        ControlsPanel = new javax.swing.JPanel();
        btnDown = new javax.swing.JButton();
        btnRight = new javax.swing.JButton();
        btnLeft = new javax.swing.JButton();
        btnUp = new javax.swing.JButton();
        btnPlus = new javax.swing.JButton();
        btnLess = new javax.swing.JButton();
        lblCoorX = new javax.swing.JLabel();
        tfCoorX = new javax.swing.JTextField();
        lblCoorY = new javax.swing.JLabel();
        tfCoorY = new javax.swing.JTextField();
        lblZoom = new javax.swing.JLabel();
        tfZoom = new javax.swing.JTextField();
        btnGuardar = new javax.swing.JButton();
        btnBack = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        lblMap.setBorder(new javax.swing.border.MatteBorder(null));
        lblMap.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                lblMapMouseDraggedEvent(evt);
            }
        });

        btnDown.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Interface/Resources/down.png"))); // NOI18N
        btnDown.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDownActionPerformed(evt);
            }
        });

        btnRight.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Interface/Resources/Right.png"))); // NOI18N
        btnRight.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRightActionPerformed(evt);
            }
        });

        btnLeft.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Interface/Resources/Left.png"))); // NOI18N
        btnLeft.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLeftActionPerformed(evt);
            }
        });

        btnUp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Interface/Resources/Up.png"))); // NOI18N
        btnUp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpActionPerformed(evt);
            }
        });

        btnPlus.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Interface/Resources/Zoom in small.png"))); // NOI18N
        btnPlus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPlusActionPerformed(evt);
            }
        });

        btnLess.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Interface/Resources/zoom out small.png"))); // NOI18N
        btnLess.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLessActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout ControlsPanelLayout = new javax.swing.GroupLayout(ControlsPanel);
        ControlsPanel.setLayout(ControlsPanelLayout);
        ControlsPanelLayout.setHorizontalGroup(
            ControlsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ControlsPanelLayout.createSequentialGroup()
                .addGroup(ControlsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(ControlsPanelLayout.createSequentialGroup()
                        .addGap(44, 44, 44)
                        .addComponent(btnUp, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnPlus, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(ControlsPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(btnLeft, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(39, 39, 39)
                        .addComponent(btnRight, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(ControlsPanelLayout.createSequentialGroup()
                        .addGap(42, 42, 42)
                        .addComponent(btnDown, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnLess, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        ControlsPanelLayout.setVerticalGroup(
            ControlsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, ControlsPanelLayout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(ControlsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btnUp, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnPlus))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(ControlsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btnRight, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnLeft, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(ControlsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnDown)
                    .addComponent(btnLess))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        lblCoorX.setText("Coor X:");

        tfCoorX.setText("0");
        tfCoorX.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfCoorXActionPerformed(evt);
            }
        });

        lblCoorY.setText("Coor Y:");

        tfCoorY.setText("0");
        tfCoorY.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfCoorYActionPerformed(evt);
            }
        });

        lblZoom.setText("Zoom:");

        tfZoom.setText("0");
        tfZoom.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfZoomActionPerformed(evt);
            }
        });

        btnGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Interface/Resources/1353192352_note2.png"))); // NOI18N
        btnGuardar.setText("Seleccionar");
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });

        btnBack.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Interface/Resources/1353192770_agt_back.png"))); // NOI18N
        btnBack.setText("Back");
        btnBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBackActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblMap, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(ControlsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(lblCoorX)
                                        .addGap(18, 18, 18)
                                        .addComponent(tfCoorX, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(lblCoorY)
                                            .addComponent(lblZoom))
                                        .addGap(18, 18, 18)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(tfZoom, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(tfCoorY, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btnGuardar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(btnBack)))
                        .addGap(18, 18, 18))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(ControlsPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(tfCoorX, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblCoorX))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(tfCoorY, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblCoorY))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(tfZoom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblZoom))
                        .addGap(37, 37, 37)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnGuardar)
                            .addComponent(btnBack))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblMap, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnRightActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRightActionPerformed
        if(validateTF(tfCoorX.getText())  && validateTF(tfCoorY.getText()) && validateTF(tfZoom.getText())){
            GetGoogleMap a = new GetGoogleMap();
            
            float exponente = Float.parseFloat(tfZoom.getText());
            float coorx = (float) (Float.parseFloat(tfCoorX.getText())+ 4/Math.pow(2, exponente));
            if(coorx >= 180){
                coorx = coorx - 180;
            }
            tfCoorX.setText(String.valueOf(coorx));
            lblMap.setIcon(new ImageIcon(a.GetMap(tfCoorX.getText(), tfCoorY.getText(), tfZoom.getText())));
        }else{
            JOptionPane.showMessageDialog(frame, "Por favor ingrese datos válidos");            
        }
    }//GEN-LAST:event_btnRightActionPerformed

    private void tfZoomActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfZoomActionPerformed
        reloadMap();
    }//GEN-LAST:event_tfZoomActionPerformed

    private void tfCoorYActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfCoorYActionPerformed
        reloadMap();
    }//GEN-LAST:event_tfCoorYActionPerformed

    private void tfCoorXActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfCoorXActionPerformed
        reloadMap();
    }//GEN-LAST:event_tfCoorXActionPerformed

    private void btnLeftActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLeftActionPerformed
        if(validateTF(tfCoorX.getText())  && validateTF(tfCoorY.getText()) && validateTF(tfZoom.getText())){
            GetGoogleMap a = new GetGoogleMap();
            
            float exponente = Float.parseFloat(tfZoom.getText());
            float coorx = (float) (Float.parseFloat(tfCoorX.getText())- 4/Math.pow(2, exponente));
            if(coorx <= -180){
                coorx = coorx + 180;
            }
            tfCoorX.setText(String.valueOf(coorx));
            lblMap.setIcon(new ImageIcon(a.GetMap(tfCoorX.getText(), tfCoorY.getText(), tfZoom.getText())));
        }else{
            JOptionPane.showMessageDialog(frame, "Por favor ingrese datos válidos");            
        }
    }//GEN-LAST:event_btnLeftActionPerformed

    private void btnUpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpActionPerformed
        if(validateTF(tfCoorX.getText())  && validateTF(tfCoorY.getText()) && validateTF(tfZoom.getText())){
            GetGoogleMap a = new GetGoogleMap();
            
            float exponente = Float.parseFloat(tfZoom.getText());
            float coory = (float) (Float.parseFloat(tfCoorY.getText())+ 4/Math.pow(2, exponente));
            if(coory >= 90){
                coory = coory - 90;
            }
            
            tfCoorY.setText(String.valueOf(coory));
            lblMap.setIcon(new ImageIcon(a.GetMap(tfCoorX.getText(), tfCoorY.getText(), tfZoom.getText())));
        }else{
            JOptionPane.showMessageDialog(frame, "Por favor ingrese datos válidoss");            
        }
    }//GEN-LAST:event_btnUpActionPerformed

    private void btnDownActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDownActionPerformed
        if(validateTF(tfCoorX.getText())  && validateTF(tfCoorY.getText()) && validateTF(tfZoom.getText())){
            GetGoogleMap a = new GetGoogleMap();
            
            float exponente = Float.parseFloat(tfZoom.getText());
            float coory = (float) (Float.parseFloat(tfCoorY.getText())- 4/Math.pow(2, exponente));
            if(coory <= -90){
                coory = coory + 90;
            }
            tfCoorY.setText(String.valueOf(coory));
            lblMap.setIcon(new ImageIcon(a.GetMap(tfCoorX.getText(), tfCoorY.getText(), tfZoom.getText())));
        }else{
            JOptionPane.showMessageDialog(frame, "Por favor ingrese datos válidos");            
        }
    }//GEN-LAST:event_btnDownActionPerformed

    private void btnPlusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPlusActionPerformed
        if(validateTF(tfCoorX.getText())  && validateTF(tfCoorY.getText()) && validateTF(tfZoom.getText())){
            GetGoogleMap a = new GetGoogleMap();
            
            int zoom = Integer.parseInt(tfZoom.getText()) + 1;
            if(zoom >= 18){
                btnPlus.setEnabled(false);
            }
            btnLess.setEnabled(true);
            tfZoom.setText(String.valueOf(zoom));
            lblMap.setIcon(new ImageIcon(a.GetMap(tfCoorX.getText(), tfCoorY.getText(), tfZoom.getText())));
        }else{
            JOptionPane.showMessageDialog(frame, "Por favor ingrese datos válidos");            
        }
    }//GEN-LAST:event_btnPlusActionPerformed

    private void btnLessActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLessActionPerformed
        if(validateTF(tfCoorX.getText())  && validateTF(tfCoorY.getText()) && validateTF(tfZoom.getText())){
            GetGoogleMap a = new GetGoogleMap();
            
            int zoom = Integer.parseInt(tfZoom.getText()) - 1;
            if(zoom <= 0){
                btnLess.setEnabled(false);
            }
            btnPlus.setEnabled(true);
            tfZoom.setText(String.valueOf(zoom));
            lblMap.setIcon(new ImageIcon(a.GetMap(tfCoorX.getText(), tfCoorY.getText(), tfZoom.getText())));
        }else{
            JOptionPane.showMessageDialog(frame, "Por favor ingrese datos válidos");            
        }
    }//GEN-LAST:event_btnLessActionPerformed

    private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed
        parent.setSatellitalImageData(tfCoorX.getText(), tfCoorY.getText(), tfZoom.getText());
        this.dispose();
    }//GEN-LAST:event_btnGuardarActionPerformed

    private void lblMapMouseDraggedEvent(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblMapMouseDraggedEvent

    }//GEN-LAST:event_lblMapMouseDraggedEvent

    private void btnBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBackActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_btnBackActionPerformed
    
    private void reloadMap(){
        if(validateTF(tfCoorX.getText())  && validateTF(tfCoorY.getText()) && validateTF(tfZoom.getText())){
            GetGoogleMap a = new GetGoogleMap();
            lblMap.setIcon(new ImageIcon(a.GetMap(tfCoorX.getText()+1, tfCoorY.getText(), tfZoom.getText())));
        }else{
            JOptionPane.showMessageDialog(frame, "Error por favor ingrese datos válidos");
        }
    }
    
    private boolean validateTF(String toValidate){
        boolean a = true;
        try{
            Float.parseFloat(toValidate);
        }catch(NumberFormatException ex){
            a = false;
        }        
        return a;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel ControlsPanel;
    private javax.swing.JButton btnBack;
    private javax.swing.JButton btnDown;
    private javax.swing.JButton btnGuardar;
    private javax.swing.JButton btnLeft;
    private javax.swing.JButton btnLess;
    private javax.swing.JButton btnPlus;
    private javax.swing.JButton btnRight;
    private javax.swing.JButton btnUp;
    private javax.swing.JLabel lblCoorX;
    private javax.swing.JLabel lblCoorY;
    private javax.swing.JLabel lblMap;
    private javax.swing.JLabel lblZoom;
    private javax.swing.JTextField tfCoorX;
    private javax.swing.JTextField tfCoorY;
    private javax.swing.JTextField tfZoom;
    // End of variables declaration//GEN-END:variables
}
